//
//  TTMusicRankListViewController.m
//  TTMusic
//
//  Created by chenpengyu on 16/3/30.
//  Copyright © 2016年 cpy. All rights reserved.
//

#import "TTMusicRankListViewController.h"
#import "CPYRankListTableViewCell.h"
#import "TTMusicRankListModel.h"
#import "CPYRankListRefsModel.h"
#import "CPYRankListSongModel.h"
#import "CPYRankListDetailViewController.h"
@interface TTMusicRankListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) TTMusicBaseTableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSMutableArray *dataRdfsSource;
@property (nonatomic, strong) NSMutableArray *dataSongSource;

@end

@implementation TTMusicRankListViewController

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:YES];
    [TTMusicAFNetworkTool GETNetWithUrl:@"http://api.songlist.ttpod.com/channels/bhb/children" body:nil headerFile:nil response:TTMusicJSON success:^(id result) {
        for (NSDictionary *dic in result) {
            TTMusicRankListModel *rankListModel = [[TTMusicRankListModel alloc] initWithDataSource:dic];
            
            [_dataSource addObject:rankListModel];
            
        }
        [_tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}
- (void)creatNavButtonItem {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]  initWithTitle:@"< 返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]];
}
- (void)leftBarButtonAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];

}
- (void)creatTableView {
    self.tableView = [[TTMusicBaseTableView alloc] initWithFrame:CGRectMake(0, 0, SysWidth, SysHeight - 64) style:UITableViewStylePlain];
    _tableView.delegate = self; 
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[CPYRankListTableViewCell class] forCellReuseIdentifier:@"CPYRankListTableViewCellIdentifier"];
    [self.view addSubview:_tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (_dataSource.count != 0) {
        return _dataSource.count;
    }else {
        return 0;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_dataSource.count != 0) {
        TTMusicRankListModel *rankListModel = _dataSource[section];
        self.dataRdfsSource = [NSMutableArray array];
        for (CPYRankListRefsModel *refsModel in rankListModel.refs) {
            [_dataRdfsSource addObject:refsModel];
        }
        return _dataRdfsSource.count;
    }else {
        return 0;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_dataSource.count != 0) {
        return SysWidth / 4;
    }else {
        return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_dataSource.count != 0) {
        TTMusicRankListModel *rankListModel = _dataSource[indexPath.section];
        CPYRankListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CPYRankListTableViewCellIdentifier"];
        CPYRankListRefsModel *refsModel = rankListModel.refs[indexPath.row];
        cell.refsModel = refsModel;
        return cell;
    }
    return nil;
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor whiteColor];
    self.dataSource = [NSMutableArray array];
    self.dataSongSource = [NSMutableArray array];
    // Do any additional setup after loading the view.
    [self creatNavButtonItem];
    [self creatTableView];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
        }
    }
    TTMusicRankListModel *rankListModel = _dataSource[indexPath.section];
    CPYRankListDetailViewController *rankListDetailVC = [[CPYRankListDetailViewController alloc] init];
    CPYRankListRefsModel *refsModel = rankListModel.refs[indexPath.row];
    rankListDetailVC.songListId = [NSString stringWithFormat:@"%@", refsModel.songlist_id];
    rankListDetailVC.imageStr = [refsModel.image objectForKey:@"pic"];
    [self.navigationController pushViewController:rankListDetailVC animated:YES];
    
}
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    return 30;
//}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SysWidth, 30)];
    UILabel *label = [[UILabel alloc] initWithFrame:headView.bounds];
    label.textAlignment = 1;
    label.font = [UIFont systemFontOfSize:23];
    TTMusicRankListModel *rankListModel = _dataSource[section];
    
    label.text = rankListModel.name;
    [headView addSubview:label];
    return headView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
